package algorithm.leetcode.primary.array;

/**
 * 买卖股票的最佳时机 II
 * 输入: [7,1,5,3,6,4]
 * 输出: 7
 * 解释: 在第 2 天（股票价格 = 1）的时候买入，
 *      在第 3 天（股票价格 = 5）的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
 *      随后，在第 4 天（股票价格 = 3）的时候买入，
 *      在第 5 天（股票价格 = 6）的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
 */
public class BuySellStocks {
    public static void main(String[] args) {
        int[] prices = {7,1,5,3,6,4};
        //int[] prices = {7,6,4,3,1};
        //int[] prices = {1,2,3,4,5};
        //int[] prices = {5,6,3,1,4};
        //int[] prices = {3,1,7,3,4,9};
        //int[] prices = {6,1,3,2,4,7};
        //examples1(prices);
        //System.out.println(calculate1(prices,0));
        System.out.println(maxProfit(prices));
    }
    public static Integer examples1(int[] prices) {
        Integer profit = 0;//最优利润
        Integer max = 0; //最大差值
        Integer next = 2; //下个索引
        Integer hBit = 0;//增高
        Integer sBit = 0;//最低
        Integer isContunueH = 0; //是否持续增高
        Integer isContunueS =  0; //是否持续降低
        if(prices.length >0 ){
            //profit = prices[0];
            //max = prices[0];
            int last = 0;
            for (int i=0;i<prices.length;i++){
                if(prices[i] > max && next < prices.length){
                    max = prices[i];
                }
                if((prices[i]  - last) > 0){//增了
                    if(isContunueH <= 0){
                        sBit = last;
                    }
                    isContunueH++;
                    hBit = prices[i];
                    isContunueS = 0;
                } else { //降了
                    if(isContunueS<=0){
                        hBit = prices[i];
                    }
                    isContunueH = 0;
                    sBit = prices[i];
                    isContunueS++;
                }
                last = prices[i];//当前值变为前一个值
            }
            System.out.println("max:"+max+",hBit:"+hBit+",sBit:"+sBit+",isContunueH:"+isContunueH+",isContunueS:"+isContunueS);
            if(isContunueH>0) {
                if(isContunueH<prices.length){
                    profit = hBit - sBit;
                } else {
                    profit = hBit - sBit - 1;
                }
            } else if(isContunueS>1) {
                if(isContunueS+1 == prices.length){
                    profit = 0;
                } else {
                    profit = hBit - sBit - 1;
                }
            } else {
                profit = max;
            }
        }
        System.out.println(profit);
        return profit;
    }

    public static int calculate1(int prices[], int s) {
        if (s >= prices.length)
            return 0;
        int max = 0;
        for (int start = s; start < prices.length; start++) {
            int maxprofit = 0;
            for (int i = start + 1; i < prices.length; i++) {
                if (prices[start] < prices[i]) {
                    int profit = calculate1(prices, i + 1) + prices[i] - prices[start];
                    if (profit > maxprofit)
                        maxprofit = profit;
                }
            }
            if (maxprofit > max)
                max = maxprofit;
        }
        return max;
    }

    public static int maxProfit(int[] prices) {
        int maxprofit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i - 1])
                maxprofit += prices[i] - prices[i - 1];
        }
        return maxprofit;
    }
}
